package banwood.com.xiongfasy.activity;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Build;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Map;

import banwood.com.xiongfasy.BASE.BaseActivity;
import banwood.com.xiongfasy.MainActivity;
import banwood.com.xiongfasy.R;
import banwood.com.xiongfasy.widget.VirtualKeyboardView;
import butterknife.BindView;
import butterknife.OnClick;


/*
 * 收款页面
 * */
public class ShouKuaiActivity extends BaseActivity   {

    private static final String TAG ="ShouKuaiActivity" ;
    @BindView(R.id.receipt_back)
    ImageView receiptBack;
    @BindView(R.id.login_exit)
    TextView loginExit;
    @BindView(R.id.tuikuai)
    TextView tuikuai;
    @BindView(R.id.query)
    TextView query;
    @BindView(R.id.money_get)
    EditText textAmount;
    @BindView(R.id.shoukuai)
    Button shoukuai;
    @BindView(R.id.virtualKeyboardView)
    VirtualKeyboardView virtualKeyboardView;
    private GridView gridView;
    private ArrayList<Map<String, String>> valueList;
    private ActionBar actionBar;


    @Override
    protected int getLayoutId() {
        return R.layout.receipt_xml;
    }

    @Override
    protected void initView() {
        actionBar = getActionBar();
        actionBar.hide();


        //设置不调用系统键盘
        if (Build.VERSION.SDK_INT <= 10) {
            textAmount.setInputType(InputType.TYPE_NULL);
        } else {
            this.getWindow().setSoftInputMode(
                    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            try {
                Class<EditText> cls = EditText.class;
                Method setShowSoftInputOnFocus;
                setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus",
                        boolean.class);
                setShowSoftInputOnFocus.setAccessible(true);
                setShowSoftInputOnFocus.invoke(textAmount, false);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
        }

        gridView = virtualKeyboardView.getGridView();
        gridView.setOnItemClickListener(onItemClickListener);

        textAmount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String text = s.toString();
                if(text.length()>0){
                    char c = text.charAt(0);
                    String s1=String.valueOf(c);
                    if(s1.equals(".")){
                        text=text.replace(".","0.");
                        textAmount.setText(text);
                        textAmount.setSelection(text.length());
                    }else if(s1.equals("0")){
                        if(text.length()>1){
                            char c1 = text.charAt(1);
                            String s2=String.valueOf(c1);
                            if(!s2.equals(".")){
                                text=text.substring(1,text.length());
                                textAmount.setText(text);
                                textAmount.setSelection(text.length());
                            }
                        }
                    }
                }
                if (text.contains(".")) {
                    int index = text.indexOf(".");
                    if (index + 3 < text.length()) {
                        text = text.substring(0, index + 3);
                        textAmount.setText(text);
                        textAmount.setSelection(text.length());
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


    }

    @Override
    protected void initData() {
        valueList = virtualKeyboardView.getValueList();
    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            if (position < 11 && position != 9) {    //点击0~9按钮

                String amount = textAmount.getText().toString().trim();
                amount = amount + valueList.get(position).get("name");

                textAmount.setText(amount);

                Editable ea = textAmount.getText();
                textAmount.setSelection(ea.length());
            } else {

                if (position == 9) {      //点击退格键
                    String amount = textAmount.getText().toString().trim();
                    if (!amount.contains(".")) {
                        amount = amount + valueList.get(position).get("name");
                        textAmount.setText(amount);

                        Editable ea = textAmount.getText();
                        textAmount.setSelection(ea.length());
                    }
                }

                if (position == 11) {      //点击退格键
                    String amount = textAmount.getText().toString().trim();
                    if (amount.length() > 0) {
                        amount = amount.substring(0, amount.length() - 1);
                        textAmount.setText(amount);

                        Editable ea = textAmount.getText();
                        textAmount.setSelection(ea.length());
                    }
                }
            }
        }
    };

    @OnClick({R.id.query,R.id.tuikuai,R.id.shoukuai,R.id.login_exit,R.id.receipt_back})
    public void shouKuaiOnclick(View v){
        Intent intent;
        switch (v.getId()) {
            case R.id.query:
                intent = new Intent(ShouKuaiActivity.this, InquiryActivity.class);
                startActivity(intent);
                break;
            case R.id.tuikuai:
                intent = new Intent(ShouKuaiActivity.this, TuiKuaiActivity.class);
                startActivity(intent);
                break;
            case R.id.shoukuai:
                if(textAmount.getText().toString().length()>0 && !textAmount.getText().toString().trim().equals("")&&!textAmount.getText().toString().trim().equals(".")){
                    intent = new Intent(ShouKuaiActivity.this, ScanpaymentActivity.class);
                    intent.putExtra("money",textAmount.getText().toString());
                    startActivity(intent);
                    textAmount.setText("");
                }else{
                    Toast.makeText(ShouKuaiActivity.this,"请输入金额",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.login_exit:
                intent=new Intent(ShouKuaiActivity.this, MainActivity.class);
                startActivity(intent);
                MyApplication.setUserName("");
                break;
            case R.id.receipt_back:
                finish();
                break;
            default:
                break;
        }
    }




}
